package com.ruoyi.userManage.controller;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.userManage.domain.TicketConsumeDetail;
import com.ruoyi.userManage.domain.User;
import com.ruoyi.userManage.mapper.UserMapper;
import com.ruoyi.userManage.service.ITicketConsumeDetailService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 漫券消费记录Controller
 *
 * @author ruoyi
 * @date 2025-08-12
 */
@RestController
@RequestMapping("/ticketConsumeDetail/ticketConsumeDetail")
public class TicketConsumeDetailController extends BaseController {
    @Autowired
    private ITicketConsumeDetailService ticketConsumeDetailService;
    @Resource
    private UserMapper userMapper;

    /**
     * 查询漫券消费记录列表
     */
    @PreAuthorize("@ss.hasPermi('ticketConsumeDetail:ticketConsumeDetail:list')")
    @GetMapping("/list")
    public TableDataInfo list(TicketConsumeDetail ticketConsumeDetail) {
        if (!StrUtil.isEmpty(ticketConsumeDetail.getUserPhone())) {
            Long userId = userMapper.selectIdByPhone(ticketConsumeDetail.getUserPhone());
            if (userId != null) {
                ticketConsumeDetail.setUserId(userId);
            } else {
                return getDataTable(Collections.emptyList());
            }
        }

        if (ticketConsumeDetail.getSixUserId() != null ) {
            Long userId = userMapper.selectUserIdBySixUserId(ticketConsumeDetail.getSixUserId());
            if (userId != null) {
                ticketConsumeDetail.setUserId(userId);
            } else {
                return getDataTable(Collections.emptyList());
            }
        }
        startPage();
        List<TicketConsumeDetail> list = ticketConsumeDetailService.selectTicketConsumeDetailList(ticketConsumeDetail);
        if (!CollectionUtils.isEmpty(list)) {
            for (TicketConsumeDetail detail : list) {
                String phone = userMapper.selectPhoneById(detail.getUserId());
                detail.setUserPhone(!StrUtil.isEmpty(phone) ? phone : "");
                User user = userMapper.selectUserById(detail.getUserId());
                detail.setUserId(!ObjectUtils.isEmpty(user) ? user.getUserId() : 0);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出漫券消费记录列表
     */
    @PreAuthorize("@ss.hasPermi('ticketConsumeDetail:ticketConsumeDetail:export')")
    @Log(title = "漫券消费记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TicketConsumeDetail ticketConsumeDetail) {
        if (!StrUtil.isEmpty(ticketConsumeDetail.getUserPhone())) {
            Long userId = userMapper.selectIdByPhone(ticketConsumeDetail.getUserPhone());
            if (userId != null) {
                ticketConsumeDetail.setUserId(userId);
            }
        }

        if (ticketConsumeDetail.getSixUserId() != null ) {
            Long userId = userMapper.selectUserIdBySixUserId(ticketConsumeDetail.getSixUserId());
            if (userId != null) {
                ticketConsumeDetail.setUserId(userId);
            }
        }
        List<TicketConsumeDetail> list = ticketConsumeDetailService.selectTicketConsumeDetailList(ticketConsumeDetail);
        if (!CollectionUtils.isEmpty(list)) {
            for (TicketConsumeDetail detail : list) {
                String phone = userMapper.selectPhoneById(detail.getUserId());
                detail.setUserPhone(!StrUtil.isEmpty(phone) ? phone : "");
            }
        }
        ExcelUtil<TicketConsumeDetail> util = new ExcelUtil<TicketConsumeDetail>(TicketConsumeDetail.class);
        util.exportExcel(response, list, "漫券消费记录数据");
    }

    /**
     * 获取漫券消费记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('ticketConsumeDetail:ticketConsumeDetail:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(ticketConsumeDetailService.selectTicketConsumeDetailById(id));
    }

    /**
     * 新增漫券消费记录
     */
    @PreAuthorize("@ss.hasPermi('ticketConsumeDetail:ticketConsumeDetail:add')")
    @Log(title = "漫券消费记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TicketConsumeDetail ticketConsumeDetail) {
        return toAjax(ticketConsumeDetailService.insertTicketConsumeDetail(ticketConsumeDetail));
    }

    /**
     * 修改漫券消费记录
     */
    @PreAuthorize("@ss.hasPermi('ticketConsumeDetail:ticketConsumeDetail:edit')")
    @Log(title = "漫券消费记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TicketConsumeDetail ticketConsumeDetail) {
        return toAjax(ticketConsumeDetailService.updateTicketConsumeDetail(ticketConsumeDetail));
    }

    /**
     * 删除漫券消费记录
     */
    @PreAuthorize("@ss.hasPermi('ticketConsumeDetail:ticketConsumeDetail:remove')")
    @Log(title = "漫券消费记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ticketConsumeDetailService.deleteTicketConsumeDetailByIds(ids));
    }
}
